import "./index.less";
// import { useState } from "react";
import CreateAndUpdata from "./components/createAndUpdata";
import Toolbar from "@/components/ViewComponents/Toolbar";
import { useDrawer } from "@/components/Drawer";
import { Tag, message, Switch } from "antd";
import { BasicTable, useTable, BasicColumn, FormSchema, TableAction, useRender } from "@/components/Table";
import { WarehouseApi } from "@/api/erp/stock/warehouse";
import { exportToExcel } from "@/utils/util";
import { useTranslation } from "react-i18next";
import { IconEnum } from "@/enums/appEnum";
const ErpStockWarehouse = () => {
	const { t } = useTranslation();
	const en_vi = "drugStorage.inventoryManagement.warehouseInformation";
	const formSchemaTop: FormSchema[] = [
		{
			label: t(`${en_vi}.name`),
			component: "Input",
			field: "name"
		},
		{
			label: t(`${en_vi}.status`),
			component: "Select",
			field: "statusOpen",
			componentProps: {
				options: [
					{ label: t(`${en_vi}.open`), value: 0 },
					{ label: t(`${en_vi}.close`), value: 1 }
				]
			}
		}
	];
	// 配置table
	const columns: BasicColumn[] = [
		{
			title: t(`${en_vi}.name`),
			dataIndex: "name"
		},
		{
			title: t(`${en_vi}.address`),
			dataIndex: "address"
		},
		{
			title: t(`${en_vi}.whsPrice`),
			dataIndex: "whsPrice",
			render: text => parseFloat(text).toFixed(2)
		},
		{
			title: t(`${en_vi}.truckagePrice`),
			dataIndex: "truckagePrice",
			render: text => parseFloat(text).toFixed(2)
		},
		{
			title: t(`${en_vi}.principal`),
			dataIndex: "principal"
		},
		{
			title: t(`${en_vi}.remark`),
			dataIndex: "remark"
		},
		{
			title: t(`${en_vi}.sort`),
			dataIndex: "sort"
		},
		{
			title: t("felix_ui.status"),
			dataIndex: "status",
			render: (_, { status }) => (
				<>{status == 0 ? <Tag color="success">{t(`${en_vi}.open`)}</Tag> : <Tag color="error">{t(`${en_vi}.close`)}</Tag>}</>
			)
		},
		{
			title: t(`${en_vi}.defaultStatus`),
			dataIndex: "defaultStatus",
			render: (_, record) => <Switch checked={record.defaultStatus} onChange={() => onSwitchChange(record)} />
		},
		{
			title: t("felix_ui.createdTime"),
			dataIndex: "createdTime",
			render: text => useRender.renderDate(text)
		},
		{
			title: t("felix_ui.action"),
			key: "action",
			fixed: "right",
			render: (_, record: Recordable) => (
				<TableAction
					actions={[
						{
							icon: IconEnum.EDIT,
							label: t("felix_ui.edit"),
							onClick: handleEdit.bind(null, record)
						},
						{
							icon: IconEnum.DELETE,
							label: t("felix_ui.delete"),
							popConfirm: {
								title: t("common.deleteMessage"),
								placement: "left",
								confirm: handleDelete.bind(null, record.whsId)
							}
						}
					]}
				></TableAction>
			)
		}
	];
	// 配置table
	const [registerTable, { reload }] = useTable({
		api: WarehouseApi.getWarehousePage,
		columns,
		formConfig: {
			labelWidth: 120,
			schemas: formSchemaTop,
			showActionButtonGroup: true,
			showAdvancedButton: false,
			baseColProps: { span: 6 }
		},
		bordered: true,
		useSearchForm: true,
		searchInfo: {
			bizType: "1"
		}
	});
	const [registerDrawer, { openDrawer }] = useDrawer();
	//编辑弹窗
	const handleEdit = (record: Recordable) => {
		openDrawer(true, {
			reload,
			record,
			isUpdated: true
		});
	};
	//删除角色
	const handleDelete = async (id: number) => {
		// 发起删除
		await WarehouseApi.deleteWarehouse(id);
		reload();
		message.success(t("viewComponents.patientInformation.deleteSuccessfully"));
	};
	//表格导出
	const exportExcel = async () => {
		await WarehouseApi.getWarehousePage({}).then(res => {
			exportToExcel(res.list, t("felix_ui.pat_basic_info"));
		});
	};
	// 处理开关变化的函数
	const onSwitchChange = async (row: any) => {
		// 发起修改状态
		await WarehouseApi.updateWarehouseDefaultStatus(row.whsId, !row.defaultStatus);
		// 刷新列表
		reload();
	};
	const handlerToolbarAction = async (key: string) => {
		switch (key) {
			case "add":
				openDrawer(true, {
					reload,
					isUpdated: false
				});
				break;
			case "export":
				exportExcel();
				break;
		}
	};
	return (
		<div className="page">
			<div className="page-content">
				<BasicTable register={registerTable}></BasicTable>
				<CreateAndUpdata register={registerDrawer}></CreateAndUpdata>
			</div>
			<Toolbar action={handlerToolbarAction} btnList={["add", "export"]} />
		</div>
	);
};
export default ErpStockWarehouse;
